package org.jiajie.coffeecodes.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Condition Demo示例
 *
 * @author jay
 */
public class ReentrantLockDemo {
    static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        Condition condition = lock.newCondition();

        new Thread(()->{
            lock.lock();
            try {
                System.out.println("Thread-1加锁成功");
                System.out.println("Thread-1执行await被挂起");
                condition.await();
                System.out.println("Thread-1被唤醒成功");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println("Thread-1释放锁成功");
            }
        }).start();

        new Thread(()->{
            lock.lock();
            try {
                System.out.println("THread-2加锁成功");
                condition.signal();
                System.out.println("Thread-2唤醒Thread-1");
            }finally {
                lock.unlock();
                System.out.println("Thread-2释放锁成功");
            }
        }).start();
    }
}
